//
//  PersonPopViewController.m
//  嘀嘀拍
//
//  Created by 陈一航 on 16/3/3.
//  Copyright (c) 2016年 wangjia_01. All rights reserved.
//

#import "PersonPopViewController.h"
#import "PopCell.h"
static NSString *staticCell=@"PopCell";

@interface PersonPopViewController ()<UITableViewDelegate,UITableViewDataSource>

{

    NSInteger pageNum;
    
}
@property (weak, nonatomic) IBOutlet UIScrollView *popMainScoll;
@property (weak, nonatomic) IBOutlet UITableView *mListView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *mListViewHC;
@property (nonatomic,strong)NSMutableArray *dataArray;
@end

@implementation PersonPopViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _dataArray=@[].mutableCopy;
    _mListView.delegate=self;
    _mListView.dataSource=self;
    [_mListView registerNib:[UINib nibWithNibName:@"PopCell" bundle:nil] forCellReuseIdentifier:staticCell];


    pageNum=1;
    
    MJRefreshNormalHeader *refreshHeader=[MJRefreshNormalHeader  headerWithRefreshingTarget:self refreshingAction:@selector(headerRefresh)];
    
    
    [refreshHeader beginRefreshing];
    [_popMainScoll setMj_header:refreshHeader];
    
    
    MJRefreshBackNormalFooter *refreshFooter=[MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRefresh)];
    
    [_popMainScoll setMj_footer:refreshFooter];

    
    // Do any additional setup after loading the view from its nib.
}


#pragma mark  添加上啦加载 下拉刷新
- (void)headerRefresh
{
    pageNum=1;
    [self getDataSource:YES];
    
}

- (void)footerRefresh
{
    pageNum++;
    [self getDataSource:NO];
    
}

- (void)getDataSource:(BOOL)isFirst
{
    WC;
    
    [httpRequest getPersonPopNum:[NSString stringWithFormat:@"%ld",pageNum] Complement:^(NSDictionary *dic) {
        
    
        DLog(@"%@",dic);
        
        if ([dic[@"code"] integerValue]==200) {
            if (isFirst) {
                [weakSelf.dataArray removeAllObjects];
                
            }

            NSArray *array=dic[@"data"];
            if (![array isEqual:[NSNull null]]) {
                for (int i=0; i<array.count; i++) {
                    [weakSelf.dataArray addObject:array[i]];
                }
                
            }else
            {
                [SVProgressHUD showInfoWithStatus:@"暂无数据"];
            }
            
            
            weakSelf.mListViewHC.constant=88*weakSelf.dataArray.count;
        }
         [weakSelf.mListView reloadData];
        [weakSelf.popMainScoll.mj_header endRefreshing];
        [weakSelf.popMainScoll.mj_footer endRefreshing];
    }];
    
}



#pragma mark -表格的数据源和代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    PopCell *popCell=[tableView dequeueReusableCellWithIdentifier:staticCell forIndexPath:indexPath];
     popCell.selectionStyle=UITableViewCellSelectionStyleNone;
    
   
    if (indexPath.row==0) {
         [popCell.leftPop setImage:[UIImage imageNamed:@"第一名"] forState:UIControlStateNormal];
        [popCell.guanzhuImg setImage:[UIImage imageNamed:@"赞1"]];
    }
 

        if (indexPath.row==1) {
            [popCell.leftPop setImage:[UIImage imageNamed:@"第二名"] forState:UIControlStateNormal];
            [popCell.guanzhuImg setImage:[UIImage imageNamed:@"赞2"]];
        }


        if (indexPath.row==2) {
            [popCell.leftPop setImage:[UIImage imageNamed:@"第三名"] forState:UIControlStateNormal];
            [popCell.guanzhuImg setImage:[UIImage imageNamed:@"赞3"]];
        }

    
    if (indexPath.row>2) {
        
        [popCell.leftPop setTitle:[NSString stringWithFormat:@"%ld",indexPath.row+1] forState:UIControlStateNormal];

    }
    
    if (self.dataArray.count>0) {
        popCell.guanzhuBtn.tag=indexPath.row;
        [popCell.guanzhuBtn addTarget:self action:@selector(guanzhuThePerson:) forControlEvents:UIControlEventTouchUpInside];
        DLog(@"%@",self.dataArray[indexPath.row][@"thanks_count"]);
        popCell.guanzhuNum.text=[NSString stringWithFormat:@"%@",self.dataArray[indexPath.row][@"thanks_count"]];
        popCell.userName.text=[NSString stringWithFormat:@"%@",self.dataArray[indexPath.row][@"user_name"]];
        [isLogin setVendorImageUrl:popCell.userPhoto :[isLogin isHttpHeader:[isLogin isNull:self.dataArray[indexPath.row][@"avatar_file"]]]];
        popCell.userDesc.text=[NSString stringWithFormat:@"%@",[isLogin isNull:self.dataArray[indexPath.row][@"signature"]]];
        
        if ([[isLogin isNull:self.dataArray[indexPath.row][@"focus"]] integerValue]==1) {
            [popCell.isGuanZhu setText:@"取消关注"];
            
        }else
        {
            [popCell.isGuanZhu setText:@"关注"];
        }
    }
    
    return popCell;

}

- (void)guanzhuThePerson:(UIButton *)button
{
   
    [httpRequest guanZhuISSuccess:self.dataArray[button.tag][@"uid"] Complement:^(NSDictionary *dic) {
        if ([dic[@"code"] integerValue]==200) {
             [self getDataSource:YES];
        }else
        {
            [SVProgressHUD showErrorWithStatus:dic[@"message"]];
        }
    }];

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{


    return 88;
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
